package com.turbo.cloud.ums.web.logic;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.turbo.cloud.core.exception.BusinessException;
import com.turbo.cloud.core.security.utils.SecurityUtil;
import com.turbo.cloud.core.utils.BeanListUtils;
import com.turbo.cloud.ums.web.entity.UmsDept;
import com.turbo.cloud.ums.web.model.dept.UmsDeptDO;
import com.turbo.cloud.ums.web.model.dept.UmsDeptVO;
import com.turbo.cloud.ums.utils.DeptTreeUtil;
import com.turbo.cloud.ums.web.service.IUmsDeptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 逻辑处理类
 * </p>
 *
 * @author zhangluning
 * @since 2022-08-13
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class DeptLogic {

    final IUmsDeptService umsDeptService;

    /**
     * 查询部门列表
     *
     * @return 树数据
     */
    public List<UmsDeptVO> queryDeptList() {
        String systemId = SecurityUtil.getCurrentSystemId();

        LambdaQueryWrapper<UmsDept> deptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptLambdaQueryWrapper.eq(UmsDept::getSystemId, systemId);
        deptLambdaQueryWrapper.orderByAsc(UmsDept::getSort);

        List<UmsDept> deptList = umsDeptService.list(deptLambdaQueryWrapper);

        List<UmsDeptVO> resultList = new ArrayList<>();
        BeanListUtils.copyList(deptList, resultList, UmsDeptVO::new);
        return resultList;
    }

    /**
     * 查询部门树列表
     *
     * @return 树数据
     */
    public List<UmsDeptVO> queryDeptTreeList() {
        String systemId = SecurityUtil.getCurrentSystemId();

        LambdaQueryWrapper<UmsDept> deptLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deptLambdaQueryWrapper.eq(UmsDept::getSystemId, systemId);
        deptLambdaQueryWrapper.orderByAsc(UmsDept::getSort);
        List<UmsDept> deptList = umsDeptService.list(deptLambdaQueryWrapper);
        return DeptTreeUtil.getDeptTree(deptList);
    }

    /**
     * 新增部门
     *
     * @param umsDeptDO 实体
     * @return UmsDeptQO
     */
    public Boolean createDept(UmsDeptDO umsDeptDO) {
        if (StrUtil.isBlank(umsDeptDO.getSystemId())) {
            String systemId = SecurityUtil.getCurrentSystemId();
            umsDeptDO.setSystemId(systemId);
        }

        UmsDept umsDept = new UmsDept();
        BeanUtils.copyProperties(umsDeptDO, umsDept);

        boolean result = umsDeptService.save(umsDept);
        if (!result) {
            throw new BusinessException("新增部门失败");
        }
        return true;
    }

    /**
     * 修改部门
     *
     * @param umsDeptDO 实体
     * @return UmsDeptQO
     */
    public Boolean modifyDept(UmsDeptDO umsDeptDO) {
        UmsDept umsDept = new UmsDept();
        BeanUtils.copyProperties(umsDeptDO, umsDept);

        boolean result = umsDeptService.updateById(umsDept);
        if (!result) {
            throw new BusinessException("修改部门失败");
        }
        return true;
    }

    /**
     * 删除部门
     *
     * @param id 部门ID
     * @return boolean
     */
    public boolean removeDeptById(String id) {
        if (StrUtil.isBlank(id)) {
            throw new BusinessException("部门ID不能为空");
        }

        boolean result = umsDeptService.removeById(id);
        if (!result) {
            throw new BusinessException("修改部门失败");
        }
        return true;
    }

}
